<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            position: relative;
        }

        .box .tx {
            position: absolute;
            left: 520px;
            top: 90px;
        }

        #tx {
            margin-left: 600px;
            width: 200px;
            height: 30px;
            outline: none;
            margin-top: 100px;
            transition: all .3s;
            /* border: 1px solid blue; */
            resize: none;
        }

        #tx:focus {
            width: 300px;
            height: 50px;
        }

        .total {
            margin-left: 840px;
            opacity: 0;
        }

        img {
            width: 50px;
            border-radius: 50%;
        }


        .head .left {
            margin-right: 20px;
            float: left;
        }

        .head .right {
            float: left;
        }

        .head .right .top {
            color: red;
            margin-bottom: 5px;
        }

        .last {
            position: relative;
            width: 500px;
            height: 200px;
            /* border: 1px solid black; */
            margin: 10px 600px;
            display: none;
            /* opacity: 0; */
        }

        .last .content:hover {
            color: red;
            cursor: pointer;
        }

        .last .content {
            width: 430px;
            height: 140px;
            position: absolute;
            bottom: 0;
            left: 70px;
            color: black;
            opacity: 1;
            white-space: normal;
            overflow: auto;
            resize: none;
            border: transparent;
            /* background-color: deepskyblue; */
        }

        /* 
        .last button {
            position: absolute;
            bottom: 0;
            right: 0;
        } */

        a {
            position: fixed;
            right: 50%;
            bottom: 5%;
            text-decoration: none;
            color: #000;
        }

        a:hover {
            color: red;
        }
    </style>

</head>

<body>
    <div class="box">
        <div class="tx">
            <img src="imges/tx.jpg" alt="">
        </div>
        <textarea id="tx" cols="20" rows="10" placeholder="发布一条友善的评论"></textarea>
        <div class="total">
            0/200字
        </div>
        <div class="last">
            <div class="head">
                <div class="left">
                    <img src="imges/tx.jpg" alt="">
                </div>
                <div class="right">
                    <p class="top">
                        用户名
                    </p>
                    <p class="bottom">
                        发表时间
                    </p>
                </div>
            </div>
            <textarea class="content">
                评论内容
            </textarea>
            <!-- <button>删除</button> -->
        </div>

    </div>
    <div>
        <a href="首页.html">返回首页</a>

    </div>
    <script>
        let arr = []
        const tx = document.querySelector('#tx')
        const total = document.querySelector('.total')
        const content = document.querySelector('.content')
        const TOP = document.querySelector('.top')
        const last = document.querySelector('.last')
        const bottom = document.querySelector('.bottom')
        const user =JSON.parse( localStorage.getItem('user'))
        arr = JSON.parse(localStorage.getItem('arr')) || []
        console.log(arr);
        let Time
        if (arr.length !== 0) {
             last.style.display = 'block'
            Time = arr[arr.length - 1].time
            content.innerHTML = arr[arr.length - 1].content
            TOP.innerHTML = arr[arr.length - 1].username 
        }
        function gettime(t) {
            t = t / 1000
            d = parseInt(t / 60 / 60 / 24)
            h = parseInt(t / 60 / 60 % 24)
            m = parseInt(t / 60 % 60)
            s = parseInt(t % 60)
            if (d === 0 && h === 0 && m === 0 && s >= 0) {
                return '刚刚'
            }
            // else if (d === 0 && h === 0 && m === 0) {
            //     return `${s}秒`
            // }
            else if (d === 0 && h === 0 && m >= 1) {
                return `${m}分钟前`
            }
            else if (d === 0) {
                return `${h}小时前`
            }
            else {
                return `${d}天前`
            }
        }
        function getTime(time) {
            const date = +new Date()
            const DATE = date - time
            const i = gettime(DATE)
            // let year, month, day
            // year = date.getFullYear() > 9 ? date.getFullYear() : '0' + date.getFullYear()
            // month = date.getMonth() > 9 ? date.getMonth() : '0' + date.getMonth()
            // day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate()
            return i
        }
        tx.addEventListener('focus', function () {
            total.style.opacity = 1
        })
        tx.addEventListener('blur', function () {
            total.style.opacity = 0
        })
        tx.addEventListener('input', function () {
            if (tx.value.trim() !== '')
                total.innerText = tx.value.length + '/200字'
        })
        tx.addEventListener('keyup', function (e) {
            if (e.key === 'Enter') {
                if (tx.value.trim() !== '') {
                    last.style.display = 'block'
                    TOP.innerHTML = user[0].username
                    content.innerHTML = tx.value
                    Time = +new Date()
                    bottom.innerHTML = getTime(Time)
                    arr.push({
                        username: user[0].username,
                        content: tx.value,
                        time: Time,
                    })
                    localStorage.setItem('arr', JSON.stringify(arr))
                }
                else {
                    alert('评论不能为空')
                }
                tx.value = ''
                total.innerText = '0/200字'
            }
        })
        setInterval(function () {
            bottom.innerHTML = getTime(Time)
        }, 100);
        last.addEventListener('click', function () {
            let ret = prompt('是否删除此评论')
            if (ret === '是') {
                last.style.display = 'none'
                // last.style.opacity = 0
            }
        })
    </script>
</body>

</html>